package com.mapbased.util;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: mapbased.com</p>
 *
 * @author chy_hs(Baily)
 * @version 1.0
 */
public class StringNumber
{
    public static final char[] NUM_CHARS=new char[]
        {

        '.','0','1','2','3','4','5','6','7','8','9',
        'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O'
        ,'P','Q','R','S','T','U','V','W','X','Y','Z',

        '_','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o'
        ,'p','q','r','s','t','u','v','w','x','y','z'

    };
    public static final int RADIX=NUM_CHARS.length;
    static int digit(char c)
    {
        if(c < 'A')
        {
            if(c == '.')
            {
                return 0;
            }
            return c - '/';
        }
        if(c <= 'Z')
        {
            return c - 'A' + 11;
        }
        if(c == '_')
        {
            return 37;
        }
        return c - 'a' + 38;
    }

    public static String toString(long i)
    {
        int radix=RADIX;

        char buf[]=new char[12];
        boolean negative=(i < 0);
        int charPos=11;

        if(!negative)
        {
            i= -i;
        }

        while(i <= -radix)
        {
            buf[charPos--]=NUM_CHARS[(int) - (i % radix)];
            i=i / radix;
        }
        buf[charPos]=NUM_CHARS[(int) - i];

        if(negative)
        {
            buf[--charPos]='-';
        }

        return new String(buf,charPos,(12 - charPos));

    }

    /**
     * Do't suport <0 value
     * @param i int
     * @return String
     */
    public static String toStringFixLength(int iv)
    {
//       if(i<0)
//       {
//           throw new java.lang.RuntimeException("Not Suport");
//       }
        char[] cs=new char[]
            {
            '.','.','.','.','.','.'};
        String s=toString(iv);
        for(int i= s.length()-1;i>=0;i--)
        {
            cs[i+6-s.length()]=s.charAt(i);
        }


        return new String(cs);
    }

    public static String toString(int i)
    {
        int radix=RADIX;

        char buf[]=new char[7];
        boolean negative=(i < 0);
        int charPos=6;

        if(!negative)
        {
            i= -i;
        }

        while(i <= -radix)
        {
            buf[charPos--]=NUM_CHARS[ -(i % radix)];
            i=i / radix;
        }
        buf[charPos]=NUM_CHARS[ -i];

        if(negative)
        {
            buf[--charPos]='-';
        }

        return new String(buf,charPos,(7 - charPos));
    }

    public static int parseInt(String s)
        throws NumberFormatException
    {
        int radix=RADIX;
        if(s == null)
        {
            throw new NumberFormatException("null");
        }

        int result=0;
        boolean negative=false;
        int i=0,max=s.length();
        int limit;
        int multmin;
        int digit;

        if(max > 0)
        {
            if(s.charAt(0) == '-')
            {
                negative=true;
                limit=Integer.MIN_VALUE;
                i++;
            }
            else
            {
                limit= -Integer.MAX_VALUE;
            }
            multmin=limit / radix;
            if(i < max)
            {
                digit=digit(s.charAt(i++));
                if(digit < 0)
                {
                    throw new java.lang.NumberFormatException(s);
                }
                else
                {
                    result= -digit;
                }
            }
            while(i < max)
            {
                // Accumulating negatively avoids surprises near MAX_VALUE
                digit=digit(s.charAt(i++));
                if(digit < 0)
                {
                    throw new NumberFormatException(s);
                }
                if(result < multmin)
                {
                    throw new NumberFormatException(s);
                }
                result*=radix;
                if(result < limit + digit)
                {
                    throw new NumberFormatException(s);
                }
                result-=digit;
            }
        }
        else
        {
            throw new NumberFormatException(s);
        }
        if(negative)
        {
            if(i > 1)
            {
                return result;
            }
            else
            {
                /* Only got "-" */
                throw new NumberFormatException(s);
            }
        }
        else
        {
            return -result;
        }
    }

    public static long parseLong(String s)
        throws NumberFormatException
    {
        int radix=RADIX;
        if(s == null)
        {
            throw new NumberFormatException("null");
        }

        long result=0;
        boolean negative=false;
        int i=0,max=s.length();
        long limit;
        long multmin;
        int digit;

        if(max > 0)
        {
            if(s.charAt(0) == '-')
            {
                negative=true;
                limit=Long.MIN_VALUE;
                i++;
            }
            else
            {
                limit= -Long.MAX_VALUE;
            }
            multmin=limit / radix;
            if(i < max)
            {
                digit=digit(s.charAt(i++));
                if(digit < 0)
                {
                    throw new java.lang.NumberFormatException(s);
                }
                else
                {
                    result= -digit;
                }
            }
            while(i < max)
            {
                // Accumulating negatively avoids surprises near MAX_VALUE
                digit=digit(s.charAt(i++));
                if(digit < 0)
                {
                    throw new NumberFormatException(s);
                }
                if(result < multmin)
                {
                    throw new NumberFormatException(s);
                }
                result*=radix;
                if(result < limit + digit)
                {
                    throw new NumberFormatException(s);
                }
                result-=digit;
            }
        }
        else
        {
            throw new NumberFormatException(s);
        }
        if(negative)
        {
            if(i > 1)
            {
                return result;
            }
            else
            {
                /* Only got "-" */
                throw new NumberFormatException(s);
            }
        }
        else
        {
            return -result;
        }
    }
    /**
     * Number end width
     * ms<s<m<h<d<w<M<y
     * @param s String
     * @return long
     */
    public static final long getInterval(String s)
    {
        s=s.trim();
       // char c=s.charAt(s.length()-1);
       if(s.endsWith("ms"))
       {
           return Long.parseLong(s.substring(0,s.length()-2).trim());
       }
       else if(s.endsWith("s"))
       {
           return Long.parseLong(s.substring(0,s.length()-1).trim())*1000L;
       }
       else if(s.endsWith("m"))
       {
           return Long.parseLong(s.substring(0,s.length()-1).trim())*60000L;
       }
       else if(s.endsWith("h"))
       {
           return Long.parseLong(s.substring(0,s.length()-1).trim())*3600000L;
       }
       else if(s.endsWith("d"))
       {
           return Long.parseLong(s.substring(0,s.length()-1).trim())*3600000L*24L;
       }
       else
       {
           return Long.parseLong(s)*1000L;
       }
      // throw new java.lang.RuntimeException("Unknow format:"+s);


    }

    static void tst(int l)
    {
        System.out.println(l);
        System.out.println(toString(l)+"  "
                           +toStringFixLength(l)

                           + "==" + parseInt(toStringFixLength(l))
                           + "==" + l);

    }

    public static void main(String[] args)
    {

        StringNumber stringnumber=new StringNumber();

        // for(int i=0;i < Integer.MAX_VALUE;i++)
        {
            tst(-899);
        }
    }
}
